import { t } from 'i18next'
import Alert from '@/components/Alert'
import requestMeeting, { request } from '@/plugins/axios'
import jumpPage from '@/utils/jumpPage'
import { API_KIS_ROOM_PREFIX, API_PROTAL_PREFIX } from '@/utils/constant'
import { IServerInfo, FetchUserInfo, CreateConfsParams } from './interface'
import { transServerInfo, transUserInfo } from './translator'

let currentPageInfo = { tipTimes: 0, pagePath: '', isInitPage: false }

export const fetchUserInfo = async () =>
	requestMeeting<FetchUserInfo>({
		baseURL: API_PROTAL_PREFIX,
		url: '/sso/session',
		method: 'get',
	}).then((res) => (res.success && res.data ? transUserInfo(res.data) : undefined))

export const fetchServerInfo = async () =>
	requestMeeting<IServerInfo>({
		url: '/server_info',
		method: 'get',
	}).then((res) => (res.success && res.data ? transServerInfo(res.data) : undefined))

/** 机房是否托管状态 */
export const fetchStatus = async (callback: Function) =>
	requestMeeting<{ independ: number; describe?: string }>({
		url: '/independ_status',
		method: 'get',
	}).then((res) => {
		if (res.success) {
			if (res.data?.independ) {
				Alert(res.data.describe || t('当前机房为托管状态，无法操作！'))
			} else {
				callback()
			}
		} else {
			Alert(t('当前机房为托管状态，无法操作！'))
		}
	})

/** 退出 */
export const fetchLogOut = async () =>
	requestMeeting({
		baseURL: API_PROTAL_PREFIX,
		url: '/sso/logout',
		method: 'get',
	}).then((res) => {
		jumpPage('login')
	})

/** 需要知道是否是跨级建云环境的跳转地址 */
export const fetchGetPlatformServiceUrl = async () =>
	requestMeeting<string>({
		url: '/open_url',
		method: 'get',
	}).then((res) => {
		if (res.success && res.data) {
			return res.data
		}
		return '/meeting'
	})

/** 跨级建云，需要预请求设置cookie标识*/
export const fetchSetCookie = async (
	params: {
		meeting_moid: string
		type: number
	},
	callback?: Function
) =>
	requestMeeting<string>({
		url: '/cookie_info',
		method: 'get',
		params: params,
	})
		.then((res) => {
			if (res.success && res.data) {
				try {
					var meeting_machine_link = localStorage.getItem('meeting_machine_link') as any
					if (meeting_machine_link) {
						meeting_machine_link = JSON.parse(meeting_machine_link)
					} else {
						meeting_machine_link = {
							list: {}, //会议与机房的关联表
							meeting_machine_moid: '', //存储当前最新机房的moid
						}
					}
					var meeting_machine_moid = res.data
					meeting_machine_link.list[params.meeting_moid] = meeting_machine_moid
					meeting_machine_link.meeting_machine_moid = meeting_machine_moid
					localStorage.setItem('meeting_machine_link', JSON.stringify(meeting_machine_link))
				} catch (e) {
					console.error(t('存储会议、机房关系报错'), e)
				}
			}
			callback?.()
		})
		.catch((err) => {
			callback?.()
		})

/**
 * 校验是否开启终端强认证
 * 与src\components\AuthenticationCheck组件连用
 */
export const fetchCheckAuthentication = async (meetingMoid?: string) =>
	requestMeeting<boolean>({
		url: `/mcc/${meetingMoid}/check_authentication`,
		method: 'get',
	}).then((res) => res)

export const fetchCreateConfs = async (params: CreateConfsParams) =>
	request<{ conf_id: string; meeting_id: string; machine_room_moid: string }>({
		url: '/confs',
		method: 'post',
		data: params,
	}).then((res) => res)

export const fetchCheckMccInfo = async (meetingMoid?: string) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/check_mcc`,
		method: 'get',
	}).then((res) => res)

export const fetchKisCookie = async () =>
	requestMeeting({
		url: `/kis_cookie`,
		method: 'get',
	}).then((res) => res)

/**
 * 获取会议室信息
 */
export const fetchGetMeetingRoom = async (url: string, params?: any) =>
	requestMeeting({
		baseURL: API_KIS_ROOM_PREFIX,
		url: url,
		method: 'get',
	})
		.then((res) => {
			currentPageInfo.tipTimes = currentPageInfo.tipTimes + 1 // 请求有关的meetingRoomApiProxy次数
			if (!res.success) {
				Alert(t('操作失败'))
			}
			return res
		})
		.catch((err) => err)
